package edu.ncu.entity;

import com.google.gson.GsonBuilder;
import lombok.Data;
import org.springframework.web.socket.TextMessage;

import javax.persistence.Transient;
import java.io.Serializable;
import java.util.Date;

/**
 * 消息
 */
@Data
public class Contact implements Serializable {
    private int contactId;
    private int senderId;   //发送人id
    private int receiverId; //接收消息的人id
    private String content; //发送内容
    private Date sendTime;  //发送时间
    private int type ; // 表示消息类型 0表示离线消息，1表示在线消息）

    @Transient
    private User sender;
    @Transient
    private User receiver ;

    public Contact(int senderId, int receiverId, String content, Date sendTime, User sender, User receiver) {
        this.senderId = senderId;
        this.receiverId = receiverId;
        this.content = content;
        this.sendTime = sendTime;
        this.sender = sender;
        this.receiver = receiver;
    }

    public Contact() {
    }
    public TextMessage newTextMessage() {
        return new TextMessage(
                new GsonBuilder()
                        .setDateFormat("yyyy-MM-dd HH:mm:ss")
                        .create()
                        .toJson(this)) ;
    }
}
